package cn.xd.base.ddd.value.basic;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.xd.base.persistence.dos.KeyValueDO;
import cn.xd.base.persistence.mps.KeyValueMpService;
import cn.xd.common.exception.BizException;
import cn.xd.core.ddd.value.basic.Value;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

/**
 * @author mirzaLin
 * @date 2021/7/26 14:27
 */
public class DictValueString extends Value<String> {

    public DictValueString() {
        super();
    }


    public DictValueString(String key, String dictCode) {
        super(key);
        KeyValueMpService keyValueMpService = SpringUtil.getBean(KeyValueMpService.class);
        if (ObjUtil.isNull(key)) {
            throw new BizException("字典key不能为空:" + dictCode);
        }
        keyValueMpService.getOneForLimitOneResult(Wrappers.lambdaQuery(KeyValueDO.class)
                        .eq(KeyValueDO::getDictCode, dictCode).eq(KeyValueDO::getDictKey, key))
                .isEmptyThrow("字典dictCode:" + dictCode + "不存在key:" + key + "的数据");
    }

    @Override
    public int hashCode() {
        return getValue().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof DictValueString) {
            return getValue().equals(((DictValueString) obj).getValue());
        }
        return getValue().equals(obj);
    }


    /**
     * redis key 必须勿删除
     */
    @Override
    public String toString() {
        return super.getValue();
    }
}
